// component/WaterFallView/WaterFallView.js
/**
 * 瀑布流组件
 */

var leftList = new Array();//左侧集合
var rightList = new Array();//右侧集合
var leftHight = 0, rightHight = 0, itemWidth = 0, maxHeight = 0;
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    leftList: [],//左侧集合
    rightList: [],//右侧集合
    num:'',
  },
  attached: function () {
    wx.getSystemInfo({
      success: (res) => {
        let percentage = 750 / res.windowWidth;
        let margin = 20 / percentage;
        itemWidth = (res.windowWidth - margin) / 2;
        maxHeight = itemWidth / 0.6
      }
    });
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //跳转到动态详情
    dynamic_detail:function(e){
      var id = e.currentTarget.dataset.item.id;
      // var num = this.data.num
      wx.getStorage({
        key: 'userinfo',
        success: function (res) {
          wx.navigateTo({
            url: '/pages/works_details/works_details?id=' + id
          })
          console.log(res)
        },
        fail:function(res){
          wx.navigateTo({
            url: '/pages/login/login'
          })
        }
      })


     
    },

    // 跳个人主页
    touserIcon(e){
      // console.log(e)
      var userinfo = e.currentTarget.dataset.item
      wx.getStorage({
        key: 'userinfo',
        success: function (res) {
          var obj = {
            userId: userinfo.userId,
            userNameinfo: userinfo.userName,
            userAvatarSrc: userinfo.userIcon,
            id: res.data.id,
          }
          var obj = encodeURIComponent(JSON.stringify(obj));
          wx.navigateTo({
            url: '/pages/everyinfomation/everyinfomation?obj=' + obj,
          })
        },
        fail: function (res) {
          wx.navigateTo({
            url: '/pages/login/login'
          })
        }
      })
    },
 
    //点赞
    thumbsup:function(e){
      
      console.log(e)
      // var id = e.currentTarget.dataset.item.id;
      var worksId=e.currentTarget.dataset.item.id
      var isLiked=e.currentTarget.dataset.item.isLiked
      var type=''
      if(isLiked==0){
        type=1
      }else if(isLiked==1){
        type=2
      }
      this.getSetting(worksId,type);
      
    },
    getSetting(worksId,type) {
      var that = this;
      wx.getStorage({
        key: 'userinfo',
        success: function (res) {
          that.triggerEvent('thumNumber', {worksId:worksId,type:type})
          
        },
        fail:function(res){
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
      })
    },


    /**
     * 填充数据
     */
    fillData: function (isPull, listData,num) {
      if (isPull) { //是否下拉刷新，是的话清除之前的数据
        leftList.length = 0;
        rightList.length = 0;
        leftHight = 0;
        rightHight = 0;
      }
      for (let i = 0, len = listData.length; i < len; i++) {
        let tmp = listData[i];
        tmp.width = parseInt(tmp.picWidth);
        tmp.height = parseInt(tmp.picHeight);
        tmp.itemWidth = itemWidth
        let per = tmp.width / tmp.itemWidth;
        tmp.itemHeight = tmp.height / per;
        if (tmp.itemHeight > maxHeight) {
          tmp.itemHeight = maxHeight;
        }

        if (leftHight == rightHight) {
          leftList.push(tmp);
          leftHight = leftHight + tmp.itemHeight;
        } else if (leftHight < rightHight) {
          leftList.push(tmp);
          leftHight = leftHight + tmp.itemHeight;
        } else {
          rightList.push(tmp);
          rightHight = rightHight + tmp.itemHeight;
        }
      }

      this.setData({
        leftList: leftList,
        rightList: rightList,
        num:num
      });
    },
  },
})
